package m3f.data;

import m3f.io.CachedMinibatchLoader;
import m3f.matrix.Minibatch;

public class MultimodalCachedLoader implements MultimodalDataProvider{
	
	private CachedMinibatchLoader visualLoader;
	private CachedMinibatchLoader textLoader;
	private int minibatchSize;
	
	private String vFile;
	private String tFile;
	private boolean normalizeVectors;
	
	public MultimodalCachedLoader(String vFile, String tFile, boolean normalizeVectors){
		this.vFile = vFile;
		this.tFile = tFile;
		this.normalizeVectors = normalizeVectors;
		visualLoader = new CachedMinibatchLoader(vFile, normalizeVectors);
		(new Thread(visualLoader)).start();
		textLoader = new CachedMinibatchLoader(tFile, normalizeVectors);
		(new Thread(textLoader)).start();
	}
	
	public MultimodalCachedLoader clone(){
		MultimodalCachedLoader set = new MultimodalCachedLoader(vFile, tFile, normalizeVectors);
		set.minibatchSize = this.minibatchSize;
		return set;
	}
	
	@Override
	public int getMinibatchSize(){
		return minibatchSize;
	}

	@Override
	public boolean hasMoreData() {
		return textLoader.hasData() && visualLoader.hasData();
	}

	@Override
	public Minibatch nextTextual() {
		return textLoader.nextMinibatch(minibatchSize);
	}

	@Override
	public Minibatch nextVisual() {
		return visualLoader.nextMinibatch(minibatchSize);
	}

	@Override
	public void reset(boolean randomized) {
		long seed = 0;
		if(randomized)
			seed = System.currentTimeMillis();
		visualLoader.reset(seed);
		textLoader.reset(seed);
	}

	@Override
	public void setMinibatchSize(int size){
		minibatchSize = size;
	}

	@Override
	public int matrixRows() {
		int vs = visualLoader.getMatrixRows();
		int ts = textLoader.getMatrixRows();
		if(vs != ts){
			System.err.println("ERROR: Different number of rows in visual and text matrices\n" +
					vs + " found for visual data and " + ts + " found for text data.");
			System.exit(1);
		}
		return vs;
	}

	@Override
	public int textFeatures() {
		return textLoader.getVectorSize();
	}
	
	@Override
	public int visualFeatures() {
		return visualLoader.getVectorSize();
	}
}
